package com.example.p2pinvest.fragment;

import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.p2pinvest.R;
import com.example.p2pinvest.adapter.ProductAdapter;
import com.example.p2pinvest.bean.Product;
import com.example.p2pinvest.common.AppNetConfig;
import com.example.p2pinvest.common.BaseFragment;
import com.loopj.android.http.RequestParams;

import java.util.List;

import butterknife.BindView;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/10
 * 全部理财
 * ListView的使用 : ①ListView ②BaseAdapter ③Item Layout  ④集合数据(联网获取数据)
 */
public class ProductListFragment extends BaseFragment {

    @BindView(R.id.tv_product_title)
    TextView tvProductTitle;
    @BindView(R.id.lv_product_list)
    ListView lvProductList;
    private List<Product> products;

    @Override
    protected RequestParams getParams() {
        return null;
    }

    @Override
    protected String getUrl() {
        return AppNetConfig.PRODUCT;
    }

    @Override
    protected void initData(String content) {
        //跑马灯效果
        tvProductTitle.setFocusable(true);
        tvProductTitle.setFocusableInTouchMode(true);
        tvProductTitle.requestFocus();

        JSONObject jsonObject = JSON.parseObject(content);
        Boolean success = jsonObject.getBoolean("success");
        if (success) {
            String data = jsonObject.getString("data");
            products = JSON.parseArray(data, Product.class);

            ProductAdapter adapter = new ProductAdapter(products);
            lvProductList.setAdapter(adapter);  //显示列表
        }
    }

    @Override
    protected void initTitle() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_productlist;
    }
}
